package com.ruoyi.device.service.impl;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.device.domain.DeviceAccount;
import com.ruoyi.device.mapper.DeviceAccountMapper;
import com.ruoyi.device.service.IDeviceAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 设备账号信息Service业务层处理
 * 
 * @author luogongquan
 * @date 2024-09-06
 */
@Service
public class DeviceAccountServiceImpl implements IDeviceAccountService 
{
    @Autowired
    private DeviceAccountMapper deviceAccountMapper;

    /**
     * 查询设备账号信息
     * 
     * @param deviceId 设备账号信息主键
     * @return 设备账号信息
     */
    @Override
    public DeviceAccount selectDeviceAccountByDeviceId(Long deviceId)
    {
        return deviceAccountMapper.selectDeviceAccountByDeviceId(deviceId);
    }

    /**
     * 查询设备账号信息列表
     * 
     * @param deviceAccount 设备账号信息
     * @return 设备账号信息
     */
    @Override
    public List<DeviceAccount> selectDeviceAccountList(DeviceAccount deviceAccount)
    {
        return deviceAccountMapper.selectDeviceAccountList(deviceAccount);
    }

    /**
     * 新增设备账号信息
     * 
     * @param deviceAccount 设备账号信息
     * @return 结果
     */
    @Override
    public int insertDeviceAccount(DeviceAccount deviceAccount)
    {
        //查询是否已经存在
        DeviceAccount deviceAccount1 = deviceAccountMapper.selectDeviceAccountByDeviceId(deviceAccount.getDeviceId());
        if(deviceAccount1!=null){
            return updateDeviceAccount(deviceAccount);
        }
        deviceAccount.setCreateBy(SecurityUtils.getUsername());
        deviceAccount.setCreateTime(DateUtils.getNowDate());
        return deviceAccountMapper.insertDeviceAccount(deviceAccount);
    }

    /**
     * 修改设备账号信息
     * 
     * @param deviceAccount 设备账号信息
     * @return 结果
     */
    @Override
    public int updateDeviceAccount(DeviceAccount deviceAccount)
    {
        deviceAccount.setUpdateTime(DateUtils.getNowDate());
        return deviceAccountMapper.updateDeviceAccount(deviceAccount);
    }

    /**
     * 批量删除设备账号信息
     * 
     * @param deviceIds 需要删除的设备账号信息主键
     * @return 结果
     */
    @Override
    public int deleteDeviceAccountByDeviceIds(Long[] deviceIds)
    {
        return deviceAccountMapper.deleteDeviceAccountByDeviceIds(deviceIds);
    }

    /**
     * 删除设备账号信息信息
     * 
     * @param deviceId 设备账号信息主键
     * @return 结果
     */
    @Override
    public int deleteDeviceAccountByDeviceId(Long deviceId)
    {
        return deviceAccountMapper.deleteDeviceAccountByDeviceId(deviceId);
    }
}
